package com.hehui.module.customer.controller;

import com.hehui.common.core.commom.AjaxResult;
import com.hehui.common.core.controller.BaseController;
import com.hehui.common.core.page.TableDataInfo;
import com.hehui.common.utils.SecurityUtils;
import com.hehui.common.utils.StringUtils;
import com.hehui.module.customer.entity.BaseCustomer;
import com.hehui.module.customer.entity.CustomerGroup;
import com.hehui.module.customer.entity.CustomerType;
import com.hehui.module.customer.service.ICustomerGroupService;
import com.hehui.module.customer.service.ICustomerService;
import com.hehui.module.customer.service.ICustomerTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/customergroup")
public class CustomerGroupController extends BaseController {

    @Autowired
    private ICustomerGroupService customerGroupService;

    /*分页查询客户分组列表
     */
    @GetMapping("/group/list")
    public TableDataInfo customerGrouplist(CustomerGroup customerGroup) {
        startPage();
        List<CustomerGroup> list = customerGroupService.selectCustomerGroup(customerGroup);
        return getDataTable(list);
    }


    /*
     *根据分组ID 查询分组信息
     */
    @GetMapping(value = {"/", "/{customerGroupId}"})
    public AjaxResult getCustomerGroupInfoById(@PathVariable(value = "customerGroupId", required = false) Long custonerGroupId) {
        AjaxResult ajax = AjaxResult.success();
        CustomerGroup group = new CustomerGroup();
        group.setCgGroupid(custonerGroupId);
        List<CustomerGroup> list = customerGroupService.selectCustomerGroup(group);
        group = list.get(0);
        ajax.put("customerGroup", group);
        return ajax;
    }

    /*
     * 添加用户分组
     */
    @PostMapping("/group/add")
    public AjaxResult addCustomerGroup(@Validated @RequestBody CustomerGroup customerGroup) {
        //要判断   分组名是否存在
        return toAjax(customerGroupService.insertCustomerGroup(customerGroup));
    }

    /*
     * 删除分组信息
     */
    @DeleteMapping(value = {"/group", "/{customerGroupId}"})
    public AjaxResult deleteCustomerGroup(@PathVariable(value = "customerGroupId", required = false) Long customerGroupId) {

        return toAjax(customerGroupService.deleteCustomerGroupById(customerGroupId));
    }



}
